﻿using LdtJudge.DataEngine.Model.Collection;
using LdtJudge.DataEngine.Model.DataModel.ElementNode;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using LdtJudge.DataEngine.Model.ElmentModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Ldt.WindowsFormsApp
{
    public partial class HistoryFrom : Form
    {
        private string xml = "";
        private List<RequestDataModel> requestDatas = new List<RequestDataModel>();
        private List<ElementNodeModel> elementNodes { get; set; }

        /// <summary>
        /// 优选可追溯xml
        /// </summary>
        public string zsXml { get; set; }

        public HistoryFrom(string xmlDocument,object requestList=null,List<ElementNodeModel> elementNodeList=null)
        {
            InitializeComponent();
            xml = xmlDocument;
            if (requestList==null)
            {
                tabPage1.Parent = null;
            }
            else
            {
                requestDatas.Clear();
                requestDatas = requestList as List<RequestDataModel>;
                tabPage1.Parent = tabControl1;
            }
            if(elementNodeList==null || !elementNodeList.Any())
            {
                tabPage3.Parent = null;
            }
            else
            {
                elementNodes = elementNodeList;
                tabPage3.Parent = tabControl1;
            }


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HistoryFrom_Load(object sender, EventArgs e)
        {
            textBox1.Text = xml;
            textBox1.Focus();

            textBox1.SelectionStart = 1;  //设置起始位置 

            textBox1.SelectionLength = textBox1.Text.Length;  //设置长度

            if (!string.IsNullOrWhiteSpace(zsXml))
                uiZsxml.Text = zsXml;
        }

        

        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textBox1.Text))
            {
                textBox1.SelectionStart = 0;
                textBox1.SelectionLength = textBox1.Text.Length;
            }
        }

        
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.tabControl1.SelectedIndex)
            {
                case 0:

                    break;
                case 1:
                    if (requestDatas != null && requestDatas.Count>0)
                    {
                        RequestComboBox.DataSource = requestDatas;
                        RequestComboBox.SelectedIndexChanged -= RequestComboBox_SelectedIndexChanged;
                        RequestComboBox.ValueMember = "RequestKey";
                        RequestComboBox.DisplayMember = "RequestKey";
                        RequestComboBox.SelectedIndexChanged += RequestComboBox_SelectedIndexChanged;
                    }
                    break;
                case 2:
                    if(elementNodes!=null && elementNodes.Any())
                    {
                        LoadElementNodeTreeData();
                    }
                    break;
            }
        }

        private void RequestComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            UrlRichTextBox.Clear();
            ParamRichTextBox.Clear();
            ResultRichTextBox.Clear();
            var selectedRequest = this.RequestComboBox.SelectedItem as RequestDataModel;
            if (selectedRequest != null)
            {

                if (!string.IsNullOrWhiteSpace(selectedRequest.RequestUrl))
                {
                    UrlRichTextBox.Text = selectedRequest.RequestUrl;
                }


                if (!string.IsNullOrWhiteSpace(selectedRequest.RequestParam))
                {
                    ParamRichTextBox.Text = selectedRequest.RequestParam;
                }


                if (!string.IsNullOrWhiteSpace(selectedRequest.RequestResult))
                {
                    ResultRichTextBox.Text = selectedRequest.RequestResult;
                }
            }
        }

        /// <summary>
        /// 加载案件要素节点树结构
        /// </summary>
        private void LoadElementNodeTreeData()
        {
            if(elementNodes!=null && elementNodes.Any())
            {
                elementNodeTree.Nodes.Clear();
                var rootNode = elementNodes.Where(x => x.ParentId == "0").FirstOrDefault();
                if(rootNode!=null)
                {
                    TreeNode topNode = new TreeNode();
                    topNode.Name = rootNode.Id;
                    topNode.Text = rootNode.Name;
                    BindSubElementNode(topNode, rootNode.Id, elementNodes);
                    elementNodeTree.Nodes.Add(topNode);
                }
            }
        }

        /// <summary>
        /// 加载案件要素子节点数据
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="parentId"></param>
        /// <param name="allNodes"></param>
        private void BindSubElementNode(TreeNode parentNode,string parentId,List<ElementNodeModel> allNodes)
        {
            parentNode.Nodes.Clear();
            var subElements = allNodes.Where(x => x.ParentId == parentId).ToList();
            if(subElements!=null && subElements.Any())
            {
                foreach (var sub in subElements)
                {
                    var NodeTemp = new TreeNode();
                    NodeTemp.Name = sub.Id;
                    NodeTemp.Text = sub.Name;
                    parentNode.Nodes.Add(NodeTemp);
                    if (allNodes.Any(x => x.ParentId == sub.Id))
                    {
                        BindSubElementNode(NodeTemp, sub.Id, allNodes);
                    }
                }
            }
        }

        private void elementNodeTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode item = elementNodeTree.SelectedNode;
            if (item != null)
            {
                var rootNode = elementNodes.Where(x => x.ParentId == "0").FirstOrDefault();
                
                if (item.Name == rootNode.Id)
                {
                    //这里表示是根节点，不进行触发事件。
                    return;
                }
                ElementNodeModel node = elementNodes.FirstOrDefault(a => a.Id == item.Name);
                //提取前的原文值
                orgOldRichTextBox.Clear();
                if(!string.IsNullOrWhiteSpace(node.OrigianlTextStr))
                    orgOldRichTextBox.Text = node.OrigianlTextStr;
                //提取值
                orgNewRichTextBox.Clear();
                
                //提取规则编号
                nodeRuleRichTextBox.Clear();
                //提取参数
                nodeParamRichTextBox.Clear();
                if (node.interactiveModel!=null && node.interactiveModel.RuleInfo!=null)
                {
                    var modelDic = node.interactiveModel.ModelDic;
                    List<string> orgNewList = new List<string>();
                    if(modelDic.ContainsKey("originaltext"))
                    {
                        var extractValue = modelDic["originaltext"]?.FirstOrDefault();
                        orgNewList.Add($"提取值:{extractValue}");
                    }
                    if(modelDic.ContainsKey("reg"))
                    {
                        var convertValue = modelDic["reg"]?.FirstOrDefault();
                        convertValue = node.OriginalTextList.FirstOrDefault();
                        orgNewList.Add($"转换值:{convertValue}");
                    }
                    if(orgNewList!=null && orgNewList.Any())
                    {
                        orgNewRichTextBox.Text = string.Join(',', orgNewList);
                    }
                    var ruleInfo = node.interactiveModel.RuleInfo;
                    if (!string.IsNullOrWhiteSpace(ruleInfo.RuleCode))
                        nodeRuleRichTextBox.Text = ruleInfo.RuleCode;
                    if (!string.IsNullOrWhiteSpace(ruleInfo.RuleParameter))
                        nodeParamRichTextBox.Text = ruleInfo.RuleParameter;
                }

            }
        }
    }
}
